//
//  CouponCollectPopViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/21.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "CollectCouponCell.h"

#import "CouponCollectPopViewController.h"

@interface CouponCollectPopViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic ,strong) NSMutableArray <CommonCouponModel *> *dataArray;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *closeBtn;

@property (nonatomic ,strong) UIViewController *vc;

@property (nonatomic, assign) CGFloat   defaultHeight;

@end

@implementation CouponCollectPopViewController

- (instancetype)initDefaultHeight:(CGFloat)height vc:(UIViewController *)vc dataSource:(NSMutableArray <CommonCouponModel *>*)dataSource {
    self.dataArray = @[].mutableCopy;
    if (self = [super init]) {
        self.vc = vc;
        self.dataArray = dataSource.mutableCopy;
        self.defaultHeight = height;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initPop];
    [self initView];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.closeBtn pp_clipCorners:UIRectCornerAllCorners radius:self.closeBtn.frame.size.height/2];
        [self.view pp_clipCorners:UIRectCornerTopLeft|UIRectCornerTopRight radius:10];
    });
}

- (void)initPop {
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.contentSizeInPopup = CGSizeMake(TTScreenWidth, self.defaultHeight);
    self.popupController.navigationBarHidden = YES;
    [self.popupController.backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTap)]];
}

- (void)initView {
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //银行卡cell
    [_tableView registerNib:[UINib nibWithNibName:@"CollectCouponCell" bundle:nil] forCellReuseIdentifier:@"CollectCouponCellID"];
    
    [self.tableView reloadData];

}

-(void)closeBlockView {
    [self backgroundTap];
}

- (void)backgroundTap  {
    [self.popupController dismiss];
}


- (IBAction)closeAction:(id)sender {
    [self.popupController dismiss];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectCouponCellID" forIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return TTScreenWidth/3.5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self closeBlockView];

    if (self.selectIndex) {
        self.selectIndex(indexPath.row);
    }
}

- (NSMutableArray<CommonCouponModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}

@end
